package cn.wolfcode.crm.service.impl;


import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RequestMappingHandlerMapping rmhm;


    @Override
    public PageResult query(QueryObject qo) {

        int totalCount = permissionMapper.queryForCount(qo);
        if(totalCount == 0){
            return new PageResult();
        }
        List<Permission> list = permissionMapper.queryForList(qo);
        return new PageResult(totalCount,list);


    }

    /**
     * 加载权限
     */
    @Override
    public void reload() {
        //0.排除重复
        List<Permission> permissions = permissionMapper.selectAll();
        Set<String> set = new HashSet<>();
        for (Permission permission : permissions) {
            set.add(permission.getResource());
        }

        //获取所有的贴有requestMapper的方法
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
        Collection<HandlerMethod> values = handlerMethods.values();
        //迭代集合判断方法上是否有RequestsPermissions
        for (HandlerMethod method : values) {

            RequiresPermissions anno = method.getMethodAnnotation(RequiresPermissions.class);
            if(anno != null){
                String[] resources = anno.value();

                String resource = StringUtils.join(resources, ",");

                if(set.contains(resource)){
                    continue;
                }
                String name = method.getMethodAnnotation(PermissionName.class).value();
                Permission permission = new Permission();
                permission.setName(name);
                permission.setResource(resource);
                permissionMapper.insert(permission);

            }

        }
    }

    @Override
    public PageResult listAll() {

        List<Permission> list = permissionMapper.selectAll();

        return new PageResult(list.size(),list);
    }

    @Override
    public PageResult queryByRoleId(Long roleId) {

        List<Permission> list = permissionMapper.queryByRoleId(roleId);

        return new PageResult(list.size(),list);
    }

    @Override
    public List<String> getPermissonResourceByEmployeeId(Long employeeId) {


        List<String> list = permissionMapper.getPermissonResourceByEmployeeId(employeeId);
        return list;
    }


}
